<?php
require_once 'util/orderprocess.inc';
require_once 'util/TMailSender.inc';
require_once 'core/utils/repository.inc';

class TOrderNotifier extends TConfigurable implements IOrderPostProcessor {
	
/**
 * @var IKeyValueRepository
 */	
	protected $_ioc_shop_settings_;
/**
 * @var TMailSender
 */
	protected $_ioc_client_sender_;
	
/**
 * @var TMailSender
 */
	protected $_ioc_manager_sender_;

/**
 * @var TMSACL
 */	
	protected $_ioc_manager_storage_;
	
	protected $_conf_client_notification_subject_ = 'Thank you for order!';
	protected $_conf_manager_notification_subject_ = 'Order notification';
	
	protected $_array_managers_ = array();
	
	public function ProcessOrder(TMSOrderItem $order){
		if ($order->Client)
			$this->ClientSender->SendContext($order->Client->Email, $order, 'client_order_notification.tpl',_i($this->_conf_client_notification_subject_));
		else 
			$this->ClientSender->SendContext($order->GuestEmail, $order, 'client_order_notification.tpl',_i($this->_conf_client_notification_subject_));

		//if (empty($this->_array_managers_))
		
		$managers = $this->_array_managers_;
		
		if ($this->ShopSettings)
			if ($m = $this->ShopSettings->GetValue('OrderManagers'))
				$managers = array_unique(array_merge($managers,TConvertions::ConvertToArray($m)));
		
		$recievers = array_filter($managers,create_function('$v','return preg_match("/^\w[\w_\-\.]+\w@\w[\-_\.\w]+\w$/",$v);'));
		$managers = array_diff($managers,$recievers);
		
		if (!empty($managers)){ 	
			$man = $this->ManagerStorage->GetList(new TListOptions(array(new TCondition(TConditionType::C_EQUAL,array(new TFilterOption('Login', TConditionType::C_IN, $managers),1)))));
			foreach ($man as $m)
				$recievers[] = $m;
		}
		
		$this->ManagerSender->SendContext($recievers, $order, 'manager_order_notification.tpl',_i($this->_conf_manager_notification_subject_));
	}
}